var bpmService = {};
/** 
打开任务传入的参数
taskId, 任务ID
instanceId, 实例ID  草稿详情
defId,定义ID
nodeId: 节点ID
**/
bpmService.params;
//初始化动作
bpmService.init = function (params, fn) {
	this.params = params;

	var dataUrl = Vue.__ctx + "/bpm/task/getTaskData";// taskId=xxx
	if (!params.taskId) {
		dataUrl = Vue.__ctx + "/bpm/instance/getInstanceData";
	}

	var defer = Vue.baseService.postForm(dataUrl, params);
	Vue.tools.getResultData(defer, function (data) {
		//可以再表单中扩展配置逻辑
		data.flowRequestParam = bpmService.getSubmitFlowParam(data);

		if (fn) fn(data);
	}, "alert");

};

//获取表单的数据
bpmService.getFormData = function (custFormContext, button) {
	var validateForm = "start,agree,oppose".indexOf(button.alias) != -1;
	var formType = custFormContext.form.type;
	if (formType == 'INNER') {
		var errorMsg = Vue.formService.getValidateMsg(custFormContext);
		if (errorMsg && validateForm) {
			$.Dialog.alert("表单校验不通过！" + errorMsg, 7);
			return false;
		}
		return custFormContext.data;
	}
	if (formType == 'FRAME') {
		var iframeObj = document.getElementById("frmFrame").contentWindow;
		//尝试校验
		try {
			if (validateForm) {
				if (iframeObj.isValid() === false) {
					return false;
				}
			}
		}
		catch (e) {
		}
		//尝试取值
		try {
			return iframeObj.getData();
		} catch (e) {
			console.error("URL 表单获取 数据失败！" + e)
			return { test: "jeff" };
		}
	}
	else {
		alert("不支持的表单类型:" + formType)
	}
};

// 流程提交需要的参数
bpmService.getSubmitFlowParam = function (data) {
	if (!data) {
		alert("error");
		return;
	}

	if (data.task) {
		return { taskId: data.task.id, nodeId: data.task.nodeId, instanceId: data.task.instId };
	}

	if (data.instance) {
		return { instanceId: data.instance.id, defId: data.instance.defId };
	}

	return {
		defId: this.params.defId,
		instanceId: this.params.instanceId
	}
}
/**
 * 流程按钮解析。
 * 关于按钮样式，对话框宽高属性，不做可配置行，因为前段无法统一，
 * 但是、请求资源路径要求一致，比如/bpm/task/   /bpm/instance.
 * 前缀自己前段控制
 */
Vue.component('bpmButtons', {
	props: ['buttons', "ifagree", "defname", "form", "defnames"],

	data: function () {
		return {}
	},
	methods: {
		buttonClick: function (button) {
			if (button.name == "启动" && this.defname == "供应商流程") {
				// if (this.defName != undefined || this.defNames != undefined) {
				if (button.name == "启动" && this.defname == "供应商流程" || this.defnames == "供应商流程") {
					if (this.ifagree.paymentTerms == undefined && this.ifagree.standardcontract == undefined) {
						$.Dialog.error("您需要同意标准合同,付款条件后才可提交")
						return
					} else if (this.ifagree.paymentTerms == "同意" && this.ifagree.standardcontract == "不同意") {
						$.Dialog.error("您需要同意标准合同,付款条件后才可提交")
						return
					} else if (this.ifagree.standardcontract == "同意" && this.ifagree.paymentTerms == "不同意") {
						$.Dialog.error("您需要同意标准合同,付款条件后才可提交")
						return
					} else if (this.ifagree.standardcontract == "同意" && this.ifagree.paymentTerms == undefined) {
						$.Dialog.error("您需要同意标准合同,付款条件后才可提交")
						return
					} else if (this.ifagree.paymentTerms == "同意" && this.ifagree.standardcontract == undefined) {
						$.Dialog.error("您需要同意标准合同,付款条件后才可提交")
						return
					} else if (this.ifagree.paymentTerms == "不同意" && this.ifagree.standardcontract == "不同意") {
						$.Dialog.error("您需要同意标准合同,付款条件后才可提交")
						return
					}
				}
			}

			var ii = layer.load();
			//执行前置js
			if (!this.execuFn(button.beforeScript)) {
				layer.close(ii);
				return;
			}
			//获取流程数据
			var custFormContext = getCustFormComponent(this.$parent);

			//客户表单自定义 fn:custValid 校验
			var validateForm = "start,agree,oppose".indexOf(button.alias) != -1;
			if (validateForm && custFormContext.custValid) {
				if (custFormContext.custValid() === false) {
					layer.close(ii);
					return;
				}
			}
			// 获取在线表单数据，并校验内置校验
			var busData = bpmService.getFormData(custFormContext, button);

			if (busData === false) {//获取数据失败
				layer.close(ii);
				return;
			}
			var scope = this;
			var flowData = custFormContext.flowRequestParam;
			flowData.data = busData;
			flowData.action = button.alias;
			flowData.extendConf = {
				'nodeId': $.getParam("nodeId")
			};

			//获取更多完成动作配置
			var buttonStyle = this.getButtonStyle(button.alias);

			// 同意，反对的时候可以自动跳过录入意见
			var requiredOpinion = custFormContext.configSpecified.requiredOpinion;
			if (!requiredOpinion && "oppose,agree,start".indexOf(button.alias) == -1) {
				requiredOpinion = true;
			}

			if (button.configPage && requiredOpinion) {
				layer.close(ii);

				var height = buttonStyle.dialogHeigth || 300;
				var width = buttonStyle.dialogWidth || 600;

				var conf = { title: button.name, url: button.configPage, height: height, width: width, passData: flowData, topOpen: false };
				conf.ok = function (index, innerWindow) {
					if (!innerWindow.isValidate()) return;
					scope.postAction(flowData, innerWindow);
				}
				layer.close(ii);
				$.Dialog.open(conf);
			} else {
				if (!flowData.opinion) {
					flowData.opinion = button.name;
				}

				layer.close(ii);
				scope.postAction(flowData);
			}






		},
		postAction: function (flowData) {

			var ii = layer.load();
			// 执行动作
			var url = Vue.__ctx + (flowData.taskId ? "/bpm/task/ppcg/doCailiao" : "/bpm/instance/doAction"); var defer = Vue.baseService.post(url, flowData);
			Vue.tools.getResultMsg(defer, function (data) {
				layer.close(ii);
				window.setTimeout(function () {
					parent.layer.closeAll();
				}, 10)
				if (window.opener && window.opener.reloadGrid) {
					window.opener.reloadGrid();
				}
			}, function () {
				layer.close(ii);
			});


		},
		execuFn: function (fnStr) {
			if (!fnStr) return true;
			var parentScope = getCustFormComponent(this.$parent);

			var script = "var tempFunction = function(scope){ " + fnStr + "};"
			var result = eval(script + "tempFunction(parentScope);");
			if (result === false) return false;
			return true;
		},
		getButtonStyle: function (alias) {

			var buttonsStyle = {
				//启动
				"start": { css: "btn btn-success fa fa-send", dialogHeigth: 422, dialogWidth: 690 },
				"draft": { css: "btn btn-primary fa fa-clipboard" },
				"save": { css: "btn btn-primary fa fa-clipboard" },
				"agree": { css: "btn btn-success fa fa-check-square-o", dialogHeigth: 422, dialogWidth: 690 },
				"oppose": { css: "btn btn-primary fa fa-close", dialogHeigth: 422, dialogWidth: 690 },
				"reject": { css: "btn btn-danger fa fa-lastfm", dialogHeigth: 422, dialogWidth: 690 },
				"reject2Start": { css: "btn btn-danger fa fa-lastfm", dialogHeigth: 300, dialogWidth: 500 },
				"lock": { css: "btn btn-primary fa fa-lock" },
				"unlock": { css: "btn btn-primary fa fa-unlock" },
				"taskOpinion": { css: "btn btn-primary fa fa-navicon", dialogHeigth: '80%', dialogWidth: '75%' },
				"flowImage": { css: "btn btn-primary fa fa-image", dialogHeigth: '80%', dialogWidth: '90%' },
				"manualEnd": { css: "btn btn-danger fa fa-ioxhost", dialogHeigth: 300, dialogWidth: 500 },
				"print": { css: "btn btn-primary fa fa-print", dialogHeigth: 300, dialogWidth: 500 },
				'recall': { css: 'btn btn-warning fa fa-undo', dialogHeigth: 422, dialogWidth: 690 },
				'reminder': { css: 'btn btn-warning fa fa-bell-o', dialogHeigth: 422, dialogWidth: 690 },
				'revoke': { css: 'btn btn-danger fa fa-undo', dialogHeigth: 422, dialogWidth: 690 }
			}
			if (buttonsStyle[alias]) return buttonsStyle[alias];
			return { css: "btn btn-success " };
		}
	},
	template: '<div><span v-for="btn in buttons" :class="getButtonStyle(btn.alias).css" v-on:click="buttonClick(btn)" style="margin-right: 5px;">                                            \
		 			{{btn.name}}</span></div>',

});

function getCustFormComponent(pageComponent) {

	for (var i = 0, c; c = pageComponent.$children[i++];) {
		if (c.$options._componentTag === "ab-custom-form") {
			return c;
		}
		// url 表单则返回父页面
		if (c.$options._componentTag === "ab-url-form") {
			return pageComponent;
		}
	}
	// 不向下递归
	console.error("页面中找不到 cust-form 的组件！！！");
	return null;
}
